{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Export the mean of the SD maps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import the libraries\n",
    "import ee\n",
    "import pandas as pd\n",
    "import os\n",
    "import numpy as np\n",
    "import random\n",
    "from random import sample\n",
    "import itertools \n",
    "import geopandas as gpd\n",
    "from sklearn.metrics import r2_score\n",
    "from termcolor import colored # this is allocate colour and fonts type for the print title and text\n",
    "from IPython.display import display, HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "# initialize the earth engine API\n",
    "ee.Initialize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the basic maps that needed for the analysis\n",
    "# load the carbon concentration map\n",
    "carbonConcentration = ee.Image(\"users/leonidmoore/ForestBiomass/Biome_level_Wood_Carbon_Conentration_Map\")\n",
    "# load the two composites tha will be used in the analysis\n",
    "compositeImage =ee.Image(\"users/leonidmoore/ForestBiomass/20200915_Forest_Biomass_Predictors_Image\")\n",
    "compositeImageNew = ee.Image(\"projects/crowtherlab/Composite/CrowtherLab_Composite_30ArcSec\")\n",
    "# load the present forest cover\n",
    "presentForestCover = compositeImage.select('PresentTreeCover')\n",
    "# load the biome layer \n",
    "biomeLayer = compositeImage.select(\"WWF_Biome\")\n",
    "# define a pixel area layer with unit km2\n",
    "pixelAreaMap = ee.Image.pixelArea().divide(10000);\n",
    "# define the boundary geography reference\n",
    "unboundedGeo = ee.Geometry.Polygon([-180, 88, 0, 88, 180, 88, 180, -88, 0, -88, -180, -88], None, False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## load all the SD maps from Arnan et al., Spawn et al., and Walker et al."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "carbonDensityWK =  ee.Image(\"users/leonidmoore/ForestBiomass/WalkerMap/reprojected_Walker_map_1km\")\n",
    "# since the SD map from Arnan is biomass density, therefore, we directly transfer thre biomass density into carbon density by multiplying the carbon concentration\n",
    "carbonDensitySD =  ee.Image(\"users/leonidmoore/ForestBiomass/RemoteSensingModel/ESA_CCI_AGB_Map_bias_corrected_1km_2010\").multiply(carbonConcentration).mask(presentForestCover.gt(0))\n",
    "carbonDensityHM =  ee.Image(\"users/leonidmoore/ForestBiomass/SpawnMap/Spawn_Harmonized_AGB_density_Map_1km\").select('agb')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "# compose the  images into a multiband image\n",
    "carbonDensityImage = carbonDensityWK.addBands(carbonDensitySD).addBands(carbonDensityHM)\n",
    "# apply the reducer \n",
    "carbonDensityMean = carbonDensityImage.reduce(ee.Reducer.mean()).mask(presentForestCover.gt(0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'state': 'READY',\n",
       " 'description': 'Satellite_carbon_stock_density_mean_map_Export',\n",
       " 'creation_timestamp_ms': 1670614986478,\n",
       " 'update_timestamp_ms': 1670614986478,\n",
       " 'start_timestamp_ms': 0,\n",
       " 'task_type': 'EXPORT_IMAGE',\n",
       " 'id': 'AZT64NR3EHNM56RWHJGRA6OH',\n",
       " 'name': 'projects/earthengine-legacy/operations/AZT64NR3EHNM56RWHJGRA6OH'}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exportTask = ee.batch.Export.image.toCloudStorage(image = carbonDensitySD,\n",
    "                                                  description = 'Satellite_carbon_stock_density_mean_map_Export',\n",
    "                                                  fileNamePrefix = 'ForestBiomassExport/SD_Arnan_carbon_stock_density_mean_Map',\n",
    "                                                  region = unboundedGeo,\n",
    "                                                  bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                  crs = 'EPSG:4326',\n",
    "                                                  crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                  maxPixels = 1e13,\n",
    "                                                  fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTask.start()\n",
    "# show the task status\n",
    "exportTask.status()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
